package club.kingyin.kyblogprocore.service.impl;

import club.kingyin.commons.entity.core.Document;
import club.kingyin.commons.enums.BlogEnum;
import club.kingyin.kyblogprocore.service.TagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.kingyin.kyblogprocore.dao.DocumentDao;
import club.kingyin.kyblogprocore.service.DocumentService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Document)表服务实现类
 *
 * @author makejava
 * @since 2022-02-16 18:22:14
 */
@Service("documentService")
public class DocumentServiceImpl extends ServiceImpl<DocumentDao, Document> implements DocumentService {

    @Resource
    private DocumentDao documentDao;

    public int getViewsById(int id) {
        return documentDao.sumViewByUserId(id);
    }

    public List<Integer> getIds() {
        LambdaQueryWrapper<Document> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Document::getId);
        List<Document> documents = documentDao.selectList(queryWrapper);
        return documents.stream().map(Document::getId).collect(Collectors.toList());
    }

    @Override
    public Document getDocumentByUserIdAndId(int userid, int id) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Document::getClassify,Document::getId,Document::getTag,Document::getTitle,Document::getStatus,Document::getContext,Document::getBackground,Document::getSlogan)
                .eq(Document::getUserid,userid).eq(Document::getId,id);
        return documentDao.selectOne(queryWrapper);
    }

    public Page<Document> selectDocumentPage(Page<Document> page, Document document) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Document::getId,Document::getTitle,Document::getTag,Document::getUpdatetime,Document::getStatus);
        return getPage(page, document, queryWrapper);
    }

    public Page<Document> selectDocuments(Page<Document> page, Document document) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Document::getId,Document::getTitle,Document::getTag,Document::getUpdatetime,Document::getStatus,
                Document::getBackground,Document::getTimestamp,Document::getViews,Document::getSlogan,Document::getUserid);
        return getPage(page, document, queryWrapper);
    }

    public Page<Document> selectHome(int current) {
        Page<Document> page = new Page<>(current, 24);
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        // 幽小灵账号专门负责发布博客专项文章
        queryWrapper.ne(Document::getUserid,2022227);
        return getPageMain(page, queryWrapper);
    }

    public Page<Document> selectBottom(int current) {
        Page<Document> page = new Page<>(current, 16);
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        // 幽小灵账号专门负责发布博客专项文章
        queryWrapper.eq(Document::getUserid,2022227);
        return getPageMain(page, queryWrapper);
    }

    private Page<Document> getPageMain(Page<Document> page, LambdaQueryWrapper<Document> queryWrapper) {
        queryWrapper.ne(Document::getStatus, BlogEnum.DRAFT).orderByDesc(Document::getViews).orderByDesc(Document::getTimestamp);
        queryWrapper.select(Document::getId,Document::getTitle,Document::getTag,Document::getUpdatetime,
                Document::getBackground,Document::getTimestamp,Document::getViews,Document::getSlogan,Document::getUserid);
        return documentDao.selectPage(page, queryWrapper);
    }


    private Page<Document> getPage(Page<Document> page, Document document, LambdaQueryWrapper<Document> queryWrapper) {
        queryWrapper.orderByDesc(Document::getUpdatetime);
        queryWrapper.eq(Document::getUserid, document.getUserid());
        if (document.getTitle() != null && !document.getTitle().isEmpty()) {
            queryWrapper.like(Document::getTitle,document.getTitle());
        }
        if (document.getTag() != null && !document.getTag().isEmpty()) {
            queryWrapper.and(wrapper-> wrapper.likeRight(Document::getTag,document.getTag()).or().like(Document::getTag,"-"+document.getTag()));
        }
        if (document.getStatus() != null) {
            queryWrapper.eq(Document::getStatus, document.getStatus());
        }
        if (document.getViews() !=null) {
            if (document.getViews() > 1000) {
                // 查询阅读量最高的5篇
            }
        }
        return document.selectPage(page, queryWrapper);
    }

    public int containsIdByTitleAndUserId(String title, int userid) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Document::getId).eq(Document::getUserid,userid).eq(Document::getTitle, title);
        Document document = documentDao.selectOne(queryWrapper);
        return document == null || document.getId() == null ? -1 : document.getId();
    }

    public List<Document> searchIdByTitleAndUserId(String title, int userid) {
        LambdaQueryWrapper<Document> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Document::getUserid, userid).likeRight(Document::getTitle, title).select(Document::getTitle,Document::getId);
        return documentDao.selectList(queryWrapper);
    }

    @Async
    public void updateView(Document doc) {
        if (documentDao.selectById(doc.getId()).getViews()<doc.getViews()) {
            documentDao.updateById(doc);
        }
    }


}

